package cn.mw.cmdb.timeplan.entity;

import cn.mw.cmdb.util.CMDBConstants;
import cn.mw.microMonitorCommon.enums.MonthEnum;
import cn.mw.microMonitorCommon.enums.WeekEnum;
import cn.mw.microMonitorCommon.enums.WeekIndexEnum;
import lombok.Data;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.*;

@Data
public class MwTimePlanOption {
    private List<Map> activeTime;
    private List<Map> type;
    private List<Map> week;
    private List<Map> month;
    private List<Map> weekIndex;
    private List<Map> monthType;
    private List day;

    public void init(MessageSource messageSource){
        initAddActiveTime(messageSource);
        initType(messageSource);
        initWeek(messageSource);
        initMonth(messageSource);
        initWeekIndex(messageSource);
        initMonthType(messageSource);
        initDay();
    }

    private void initAddActiveTime(MessageSource messageSource){
        if(null == activeTime){
            activeTime = new ArrayList<>();
        }

        for(ActiveTimeEnum data :ActiveTimeEnum.values()){
            Map map = new LinkedHashMap();
            map.put("code" ,data.name());
            map.put("label",data.getLabel(messageSource ,data.name()));
            activeTime.add(map);
        }
    }

    private void initType(MessageSource messageSource){
        if(null == type){
            type = new ArrayList<>();
        }

        for(TimePlanType timePlanType: TimePlanType.values()){
            Map map = new LinkedHashMap();
            map.put("code" ,timePlanType.name());
            map.put("label",timePlanType.getLabel(messageSource ,timePlanType.name()));
            type.add(map);
        }
    }

    private void initWeek(MessageSource messageSource){
        if(null == week){
            week = new ArrayList<>();
        }

        for(WeekEnum item: WeekEnum.values()){
            Map map = new LinkedHashMap();
            map.put("code" ,item.getCode());

            String label = messageSource.getMessage(CMDBConstants.TimePlanWeekPrefix + item.name().toLowerCase(),null , LocaleContextHolder.getLocale());
            map.put("label",label);
            week.add(map);
        }
    }

    private void initMonth(MessageSource messageSource){
        if(null == month){
            month = new ArrayList<>();
        }

        for(MonthEnum item: MonthEnum.values()){
            Map map = new LinkedHashMap();
            map.put("code" ,item.getCode());

            String label = messageSource.getMessage(CMDBConstants.TimePlanMonthPrefix + item.getCode(),null , LocaleContextHolder.getLocale());
            map.put("label",label);
            month.add(map);
        }
    }

    private void initWeekIndex(MessageSource messageSource){
        if(null == weekIndex){
            weekIndex = new ArrayList<>();
        }

        for(WeekIndexEnum item: WeekIndexEnum.values()){
            Map map = new LinkedHashMap();
            map.put("code" ,item.getCode());

            String label = messageSource.getMessage(CMDBConstants.TimePlanWeekIndexPrefix + item.getCode(),null , LocaleContextHolder.getLocale());
            map.put("label",label);
            weekIndex.add(map);
        }
    }

    private void initMonthType(MessageSource messageSource){
        if(null == monthType){
            monthType = new ArrayList<>();
        }

        for(MonthTypeEnum item: MonthTypeEnum.values()){
            Map map = new LinkedHashMap();
            map.put("code" ,item.name());

            String label = messageSource.getMessage(CMDBConstants.TimePlanMonthTypePrefix + item.name(),null , LocaleContextHolder.getLocale());
            map.put("label",label);
            monthType.add(map);
        }
    }

    private void initDay(){
        day = new ArrayList();
        for(int i=1;i<32;i++){
            day.add(i);
        }
    }
}
